<?
/*

    This file is part of Blue Violin.

    Blue Violin is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Blue Violin is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero Public License for more details.

    You should have received a copy of the GNU Affero Public License
    along with Blue Violin.  If not, see <http://www.gnu.org/licenses/>.

    Blue Violin  Copyright (C) 2008  Blue Violin, Inc. & Josh Watts <josh.watts@blueviolin.org>
	For more information, visit http://www.blueviolin.org/

    This program comes with ABSOLUTELY NO WARRANTY.

*/


require_once("Database.php");
require_once("Utility.php");
require_once("Security.php");

class BVImages
{
	var $m_oDatabase;
	var $m_oUtility;

	function BVImages()
	{
		$this->m_oDatabase = new Database();
		$this->m_oUtility = new Utility();
	}


	function Destruct()
	{
		$this->m_oDatabase->Destruct();
	}


	function GetLoginID()
	{
// one day we'll return to using session IDs
//
// 		$sSQL = "select login_id " .
// 			"from Sessions " .
// 			"where session_id = '" . $_SESSION["SESSION_ID"] . "'";
// 		$this->m_oDatabase->ExecuteSQL($sSQL);
// 		$aRow = $this->m_oDatabase->NextRow();
// 		$nLoginID = $aRow["login_id"];

		$sSQL = "select login_id " .
			"from Logins " .
			"where GUID = '" . $this->m_oDatabase->EscapeString($_COOKIE["BLUEVIOLIN_ID"]) . "'";
		$this->m_oDatabase->ExecuteSQL($sSQL);
		$aRow = $this->m_oDatabase->NextRow();
		$nLoginID = $aRow["login_id"];
		return $nLoginID;
	}



	function SaveImage($sImageName, $sEncodedImage)
	{
		$nImageID = -1;
		$nLoginID = $this->GetLoginID();
		$sSQL = "insert into images (login_id, name, image_base64, timestamp) values " .
			"(" . $nLoginID . ", " .
			"'" . $this->m_oDatabase->EscapeString($sImageName) . "', " .
			"'" . $this->m_oDatabase->EscapeString($sEncodedImage) . "', " .
			"now() );";
		$this->m_oDatabase->ExecuteSQL($sSQL);
		$sSQL = "select last_insert_id() as image_id;";
		$nRows = $this->m_oDatabase->ExecuteSQL($sSQL);
		if (0 != $nRows)
		{
			$aRow = $this->m_oDatabase->NextRow();
			$nImageID = $aRow["image_id"];
		}
		return $nImageID;
	}


	function GetImage($nImageID)
	{
		$nLoginID = $this->GetLoginID();
		$sSQL = "select image_base64 " .
			"from Images " .
			"where image_id = " . $nImageID . " " .
			"and login_id = " . $nLoginID;
		$nRows = $this->m_oDatabase->ExecuteSQL($sSQL);
		if (0 != $nRows)
		{
			$aRow = $this->m_oDatabase->NextRow();
			$oData = base64_decode($aRow["image_base64"]);
			header("Content-Type: image/png");
			header("Content-Length: " . strlen($oData));
			echo($oData);
		}
	}
}




?>